package com.msmall.community.wish.action.html;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.community.wish.model.Wish;
import com.msmall.community.wish.model.WishReply;
import com.msmall.community.wish.model.WishType;
import com.msmall.community.wish.service.WishManager;

public class WishAction extends HTMLBaseAction{

	private static final long serialVersionUID = 4391641753872676206L;
	//如果修改此值，需要修改页面content/html/wish/wish_view.jsp 的翻页判断
	private static final int DEFAULT_WISH_REPLY_PAGE_SIZE = 5;
	
	private Long wid;//wishid
	private Long wtid;//wishTypeid
	private Long replyid;//wishreplyid
	private String content;//愿望内容
	private String replyable;//愿望是否允许祝福 1允许
	private String wishesDataArray;//[{wishid:1,userid:1,typeid:1,tips:"star1"},{}]
	private int wdex;//显示回复时的列表INDEX
	
	private Wish wish;
	private WishType wishType;
	private WishReply reply;
	private List<WishType> types;
	private List<WishReply> replies;
	
	private WishManager wishManager;
	
	public String execute(){
//		for(int i=248; i<600; i++){
//			String c = i + "我的愿望好好好好啊啊啊 啊啊";
//			wishManager.makeWish(100000011L, c, ConstantDB.REPLY, 1L);
//		}
		if(wid != null && wid.longValue() != 0l){//选中的WISH
			wish = (Wish) wishManager.get(Wish.class, wid);
			if(wish.getEnabled().compareTo(ConstantDB.AVALIABLE) == 0){
				wishType= wish.getType();
				wtid = wishType.getTypeid();
				page.setPageSize(wishType.getThreshold() - 1);
				wishManager.getWishes(page, wtid, wid);
				page.getItems().add(wish);
				page.setPageSize(wishType.getThreshold());
			}else{//愿望过期
				page.setPageSize(wishType.getThreshold());
				wishManager.getWishes(page, wtid);
				List items = page.getItems();
				if(items != null && items.size() > 0){
					wish = (Wish) items.get(0);
					wid = wish.getWishid();
				}
			}
			
		}else{
			if(wtid != null){
				wishType = (WishType) wishManager.get(WishType.class, wtid);
			}else{
				wishType= wishManager.getTheFirstWishType();
			}
			if(wishType != null){
				wtid = wishType.getTypeid();
				page.setPageSize(wishType.getThreshold());
				wishManager.getWishes(page, wtid);
				List items = page.getItems();
				if(items != null && items.size() > 0){
					wish = (Wish) items.get(0);
					wid = wish.getWishid();
				}
			}
		}
		page.setUrl(ConstantUrl.WML_WISH_INDEX + "?wtid="+wtid);
		List<Wish> wishes = page.getItems();
		if(wishes != null && wishes.size() >0){
			StringBuilder b = new StringBuilder();
			b.append("[");
			for(int i=0; i<wishes.size(); i++){
				//{wishid:1,userid:1,typeid:1,tips:"star1"},
				Wish obj = wishes.get(i);
				b.append("{")
				.append("wishid:").append(obj.getWishid()).append(",")
				.append("userid:").append(obj.getUserid()).append(",")
				.append("typeid:").append(obj.getType().getTypeid()).append(",")
				.append("tips:'").append(obj.getTips()).append("'")
				.append("}");
				if(i < wishes.size() -1){
					b.append(",");
				}
			}
			b.append("]");
			wishesDataArray = b.toString();
		}else{
			wishesDataArray = "[]";
		}
		
		types = wishManager.getWishTypes(ConstantDB.AVALIABLE);
		return SUCCESS;
	}

	/**
	 * 通过WISHID获得WISH以及对应的WISHREPLY PAGE列表
	 */
	public String getWishInfo(){
		try{
			if(wdex <=0 )wdex = 1;
			wish = (Wish) wishManager.get(Wish.class, wid);
			replies = wishManager.getWishReplies(wid, wdex, DEFAULT_WISH_REPLY_PAGE_SIZE);
			if((replies == null || replies.size() ==0) && wdex != 0){
				//最后一页时，从第一页开始
				wdex = 1;
				replies = wishManager.getWishReplies(wid, wdex, DEFAULT_WISH_REPLY_PAGE_SIZE);
			}
		}catch(Exception e){}
		return SUCCESS;
	}
	
	//进入创建WISH页面
	//TODO 检查数量限制
	public String toMake(){
		if(wtid != null && wtid.longValue() != 0){
			try{
				wishType = (WishType) wishManager.get(WishType.class, wtid);
			}catch(Exception e){}
		}
		if(wishType == null){
			types = wishManager.getWishTypes(ConstantDB.AVALIABLE);
		}
		return SUCCESS;
	}
	//创建WISH
	//TODO 检查数量限制
	public String make(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			Short reply = ConstantDB.UNREPLY;
			if(replyable != null && "1".equals(replyable)){
				reply = ConstantDB.REPLY;
			}
			wish = wishManager.makeWish(getUserid(), content, reply, wtid);
			if(wish != null){
				buffer.append(createResult("true"))
				.append(ConstantAjax.CONTENT_PRE)
				.append(wish.getWishid())
				.append(ConstantAjax.CONTENT_SUFF);
			}else{
				throw new Exception();
			}
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	//进入创建WISH回复页面
	public String toReply(){
		return SUCCESS;
	}

	/**
	 * 创建WISH回复
	 * <return>
	 * 	<result>true></result>
	 *  <item>
	 *  	<position>1</position>
	 *  	<value>reply.replyid</value>
	 *  </item>
	 *  <item>
	 *  	<position>2</position>
	 *  	<value>reply.userid</value>
	 *  </item>
	 *  <item>
	 *  	<position>3</position>
	 *  	<value>reply.content</value>
	 *  </item>
	 * </return>
	 */
	public String reply(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			reply = wishManager.saveWishReply(getUserid(), wid, content);
			if(reply != null){
				buffer.append(createResult("true"))
				.append(ConstantAjax.CONTENT_PRE)
				.append(ConstantAjax.ITEM_PRE)
				.append(createPosition("1"))
				.append(createValue(String.valueOf(reply.getReplyid())))
				.append(ConstantAjax.ITEM_SUFF)
				.append(ConstantAjax.ITEM_PRE)
				.append(createPosition("2"))
				.append(createValue(String.valueOf(reply.getUserid())))
				.append(ConstantAjax.ITEM_SUFF)
				.append(ConstantAjax.ITEM_PRE)
				.append(createPosition("3"))
				.append(createValue(String.valueOf(reply.getContent())))
				.append(ConstantAjax.ITEM_SUFF)
				.append(ConstantAjax.CONTENT_SUFF);
			}else{
				throw new Exception();
			}
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 删除回复
	 */
	public String removeWishReply(){
		wishManager.removeWishReply(getUserid(), replyid);
		return SUCCESS;
	}
	public Long getWid() {
		return wid;
	}

	public void setWid(Long wid) {
		this.wid = wid;
	}

	public Long getWtid() {
		return wtid;
	}

	public void setWtid(Long wtid) {
		this.wtid = wtid;
	}

	public Long getReplyid() {
		return replyid;
	}

	public void setReplyid(Long replyid) {
		this.replyid = replyid;
	}

	public Wish getWish() {
		return wish;
	}

	public void setWish(Wish wish) {
		this.wish = wish;
	}

	public WishType getWishType() {
		return wishType;
	}

	public void setWishType(WishType wishType) {
		this.wishType = wishType;
	}

	public WishReply getReply() {
		return reply;
	}

	public void setReply(WishReply reply) {
		this.reply = reply;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getReplyable() {
		return replyable;
	}

	public void setReplyable(String replyable) {
		this.replyable = replyable;
	}

	public List<WishType> getTypes() {
		return types;
	}

	public void setTypes(List<WishType> types) {
		this.types = types;
	}

	public String getWishesDataArray() {
		return wishesDataArray;
	}

	public void setWishesDataArray(String wishesDataArray) {
		this.wishesDataArray = wishesDataArray;
	}

	public int getWdex() {
		return wdex;
	}

	public void setWdex(int wdex) {
		this.wdex = wdex;
	}

	public List<WishReply> getReplies() {
		return replies;
	}

	public void setReplies(List<WishReply> replies) {
		this.replies = replies;
	}

	public WishManager getWishManager() {
		return wishManager;
	}

	public void setWishManager(WishManager wishManager) {
		this.wishManager = wishManager;
	}
	
}
